package com.songtech.server.Calculation;

/**
 * Create By YINN on 2018/5/3 15:16
 * Description :
 */

import com.songtech.server.Point.PosPoint;
import javafx.geometry.Pos;
import redis.clients.jedis.GeoCoordinate;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fc.w on 2017/12/8.
 */
public class CenterPointCalUtils {

    /**
     * 根据输入的地点坐标计算中心点
     *
     * @param points 点位列表
     * @return
     */
    public static PosPoint getCenterPoint(List<PosPoint> points) {

        int total = points.size();
        double X = 0, Y = 0, Z = 0;
        for (PosPoint g : points) {
            double lat, lon, x, y, z;
            lat = g.getLat() * Math.PI / 180;
            lon = g.getLon() * Math.PI / 180;
            x = Math.cos(lat) * Math.cos(lon);
            y = Math.cos(lat) * Math.sin(lon);
            z = Math.sin(lat);
            X += x;
            Y += y;
            Z += z;
        }

        X = X / total;
        Y = Y / total;
        Z = Z / total;

        double newLon = Math.atan2(Y, X);
        double hyp = Math.sqrt(X * X + Y * Y);
        double newLat = Math.atan2(Z, hyp);

        return PosPoint.instanceFactory(newLon * 180 / Math.PI, newLat * 180 / Math.PI);
    }

    /**
     * 根据输入的地点坐标计算中心点（适用于400km以下的场合）
     *
     * @param points
     * @return
     */
    public static PosPoint getCenterPoint400(List<PosPoint> points) {
        // 以下为简化方法（400km以内）
        int total = points.size();
        double lat = 0, lon = 0;
        for (PosPoint g : points) {
            lat += g.getLat() * Math.PI / 180;
            lon += g.getLon() * Math.PI / 180;
        }
        lat /= total;
        lon /= total;
        return PosPoint.instanceFactory(lon * 180 / Math.PI, lat * 180 / Math.PI);
    }


}



